
// PHASE 4: Animation
 	        	
 			function Animation(event:Event):void{
 				var _target:int = 0;
 				var a:MouseEvent = MouseEvent(event);
 				var im:Image = Image(event.currentTarget);
 				var myCanvas:Canvas = Canvas(im.parent);
 				var child:Array = myCanvas.getChildren();
 				var total:int = child.length/2;	
 				var _x:int = a.stageX - im.parent.x; // Mouse Position
				var wid:int = Math.floor((myCanvas.width+1-_path.length)/_path.length);
				var check:int = Canvas1ImWid*_path.length+(_path.length-1);
				var pos:Number = 0;
				
				if(check<=myCanvas.width)
				{	
					_target = Math.ceil(_x/Canvas1ImWid);	
					return;
				}
				else
				{
					_target = Math.ceil(_x/wid);	
					pos = _x/wid - (_target-1);
					
				}
				
				var img_left:int = _target-1;
				var img_right:int = total-_target;				
				

				var widLeft:Number = 0;
				var widRight:Number = 0;
				
				var shiftMax:int = im.width/2-wid/2;
				var shift:int = 0;				
				
				if (img_left>img_right)	// shift left		
				{	shift = img_left/total*shiftMax*(-0.5) + pos*(-0.5)*shiftMax;	}
					
				else if(img_left<=img_right)
				{	shift = img_right/total*shiftMax*(0.5) + pos*(-0.5)*shiftMax;	}
				
			// Calculate RATIO
				var _ratio:Number = (120/wid-1)*2/(total);
				_ratio = Math.floor(_ratio*100)/100;
					
				var shiftedX:int = originalX[_target-1]+shift;
				
				fcnmove( 1,_target, shiftedX, 120);

				if(img_left!=0)
				{
					widLeft = (shiftedX-img_left)/(img_left);
					widLeft = Math.floor(widLeft);
					var _window:Array = new Array();
					var _maskX:Array = new Array();
					_window[1]=0;
					_maskX[1]=0;
											
					if( img_left%2==0 )
					{
						for(var i=1; i<_target; i++)
						{
							_window[i] = Math.round( widLeft*( 1-_ratio*(img_left/2)+_ratio/2 + (i-1)*_ratio ) );		
							if(i>1)			
							{	_maskX[i] = _maskX[i-1]+_window[i-1]+1;	}
						}
					
					}
					else
					{
						for(var i=1; i<_target; i++)
						{
							_window[i] = Math.round( widLeft* ( 1-_ratio*( (img_left-1)/2)+ (i-1)*_ratio ) );				
							if(i>1)			
							{	_maskX[i] = _maskX[i-1]+_window[i-1]+1;	}
						}
					}
					var empty:int = shiftedX - (_maskX[_target-1]+_window[_target-1]) - 1;
					var j:int = 1;
					while(j<=empty)
					{
						_window[j]++;
						j++;
					}
					fcnmove(0, 1, 0, _window[1]);

					for(var k=2; k<_target; k++)
					{
						_maskX[k] = _window[k-1] + _maskX[k-1] + 1;
						fcnmove(0, k, _maskX[k], _window[k]);
					}	
				}
				
				if(img_right!=0)
				{
					widRight = (myCanvas.width-(shiftedX+im.width)-img_right)/(img_right);
					widRight = Math.floor(widRight);
					var _window:Array = new Array();
					var _maskX:Array = new Array();
					_window[_target+1]=0;
					_maskX[_target+1]=shiftedX+Canvas1ImWid+1;
					
					if ( img_right%2 == 0)
					{
						for(var i=_target+1; i<=child.length/2; i++)
						{
							_window[i] = Math.round( widRight*( 1+_ratio*(img_right/2)-_ratio/2 - (i-1-_target)*_ratio ) );
							if(i>_target+1)
							{	_maskX[i] = _maskX[i-1]+_window[i-1]+1;	}
						}
					}
					else
					{	
						for(var i=_target+1; i<=child.length/2; i++)
						{
							_window[i] = Math.round( widRight*( 1+_ratio*( (img_right-1)/2) - (i-1-_target)*_ratio ) );
							if(i>_target+1)
							{	_maskX[i] = _maskX[i-1]+_window[i-1]+1;	}
						}
					}
					var empty:int = myCanvas.width - (_maskX[child.length/2]+_window[child.length/2]);
					var j:int = 1;
					while(j<=empty)
					{
						_window[_target+j]++;
						j++;
					}
					
					fcnmove(0, _target+1, _maskX[_target+1], _window[_target+1]);
					for(var k=_target+2; k<=child.length/2; k++)
					{
						_maskX[k] = _window[k-1] + _maskX[k-1] + 1;
						fcnmove(0, k, _maskX[k], _window[k]);
					}	
 				}
 				
 			}
 			
        	function fcnmove(signal:int, target:int, maskX:int, window:int): void {
				
				var im:Image = Image(Canvas1Im[target-1]);
				var maskCanvas:Canvas = Canvas(im.mask);

				var center:int = maskX+window/2;
				var duration:int = 1000;
				
				var n:int= target-1;
				if(signal==1) // curser target image
				{
					var move1_xTo = maskX;
					var reSize_widthTo = Canvas1ImWid;
				}
				else 
				{
					var move1_xTo = center-im.width/2;
					var reSize_widthTo = window;
				}
				move1[n].end();
	            move1[n].target = im;//event1.currentTarget;
	            move1[n].duration = duration;//duration;
	            move1[n].xFrom = im.x;
	            move1[n].yFrom = im.y;
	            move1[n].xTo = move1_xTo;//im.x+shift;
	            move1[n].yTo = im.y;
	            move1[n].play();
				
				move2[n].end();
         	    move2[n].target = maskCanvas;//myCanvas;
         	    move2[n].duration = duration;
            	move2[n].xFrom = maskCanvas.x;
                move2[n].yFrom = maskCanvas.y;
                move2[n].xTo = maskX;//center-window/2;//im.x+shift+im.width/2-mask_rad2;
             	move2[n].yTo = im.y;//maskCanvas.y;
               	move2[n].play();
				
				reSize[n].end();
				reSize[n].target = maskCanvas;
				reSize[n].widthFrom = maskCanvas.width;
				reSize[n].widthTo = reSize_widthTo;//window;
				reSize[n].duration = duration;
				reSize[n].play();
        	}
